﻿using System.Web.Mvc;
using FrontEnd.Controllers;
using FrontEnd.Models;
using Xunit;

namespace UnitTests.Controllers.Membership
{
    public class When_User_Post_Registration
    {
        [Fact]
        public void Should_Store_Registration()
        {
            const string etalonEmail = "etalon@example.com";
            const string etalonDisplayName = "Etalone Jobs";
            const string etalonPassword = "etalonpassword";

            var environment = new TestEnvironment();
            MembershipController controller = environment.Build();

            // Execution
            controller.Register(etalonEmail, etalonDisplayName, etalonPassword);

            // Verification
            AccountInfo storedAccount = environment.Membership.GetAccountByEmail(etalonEmail);
			string storedPassword = environment.Membership.GetPasswordByEmail(etalonEmail);

            Assert.NotNull(storedAccount);
            Assert.Equal(etalonEmail, storedAccount.Person.EmailAddress);
            Assert.Equal(etalonDisplayName, storedAccount.Person.DisplayName);

            Assert.NotNull(storedPassword);
            Assert.Equal(etalonPassword, storedPassword);
        }

        [Fact]
        public void Should_Redirect_To_Success()
        {
            const string fakeEmail = "fake@example.com";
            const string fakeDisplayName = "Fake Jobs";
            const string fakePassword = "fakepassword";

            var environment = new TestEnvironment();
            MembershipController controller = environment.Build();

            // Execution
            RedirectToRouteResult result = (RedirectToRouteResult)controller.Register(fakeEmail, fakeDisplayName, fakePassword);

            // Verification
            // Should redirect ot the succes page
            Assert.Equal(MembershipController.RegistrationSuccessAction, result.RouteValues["Action"]);
            // Should redirect with user accountId;
            Assert.Equal(result.RouteValues[MembershipController.RegistrationSuccessActionAccountIdArgument], fakeEmail);
        }
    }
}
